home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / c01oop.zip / ADAWKBK / SOL4-1.ADA < prev    next >
Text File  |  1992-08-25  |  2KB  |  65 lines

  1. -- Problem 4.1
  2. -- by Rick Conn
  3. with Text_IO;
  4. procedure Main is
  5.  
  6.   type COMPLEX_NUMBER is record
  7.     RP : FLOAT := 0.0;  -- initialize to 0,0
  8.     IP : FLOAT := 0.0;
  9.   end record;
  10.  
  11.   N1, N2, N3 : COMPLEX_NUMBER;
  12.  
  13.   package Float_IO is new Text_IO.Float_IO (FLOAT);
  14.  
  15.   function "+" (Left, Right : in COMPLEX_NUMBER)
  16.       return COMPLEX_NUMBER is
  17.   begin
  18.     return (Left.RP + Right.RP, Left.IP + Right.IP);
  19.   end "+";
  20.  
  21.   function "-" (Left, Right : in COMPLEX_NUMBER)
  22.       return COMPLEX_NUMBER is
  23.   begin
  24.     return (Left.RP - Right.RP, Left.IP - Right.IP);
  25.   end "-";
  26.  
  27.   function "*" (Left, Right : in COMPLEX_NUMBER)
  28.       return COMPLEX_NUMBER is
  29.   begin
  30.     return (Left.RP * Left.RP - Right.RP * Right.RP,
  31.             Left.RP * Right.IP + Left.IP * Right.RP);
  32.   end "*";
  33.  
  34.   procedure Print (Item : in COMPLEX_NUMBER) is
  35.   begin
  36.     Float_IO.Put (Item.RP, 5, 5, 0);
  37.     Text_IO.Put (" + ");
  38.     Float_IO.Put (Item.IP, 5, 5, 0);
  39.     Text_IO.Put ("j");
  40.   end Print;
  41.  
  42.   procedure Print_All is
  43.   begin
  44.     Text_IO.Put ("N1   = "); Print (N1); Text_IO.New_Line;
  45.     Text_IO.Put ("  N2 = "); Print (N2); Text_IO.New_Line;
  46.     Text_IO.Put ("  N3 = "); Print (N3); Text_IO.New_Line;
  47.   end Print_All;
  48.  
  49. begin -- Main
  50.  
  51.   Print_All;
  52.   N1 := (2.2, 4.4);
  53.   N2 := (1.0, 12.2);
  54.   N3 := N1 + N2;
  55.   Text_IO.Put_Line ("Sum:");
  56.   Print_All;
  57.   N3 := N1 - N2;
  58.   Text_IO.Put_Line ("Difference:");
  59.   Print_All;
  60.   N3 := N1 * N2;
  61.   Text_IO.Put_Line ("Product:");
  62.   Print_All;
  63.  
  64. end Main;
  65.